/**
 * 1026. 节点与其祖先之间的最大差值
 */
public class No1026 {
    int res = 0;

    public int maxAncestorDiff(TreeNode root) {
        dfs(root, Integer.MAX_VALUE, Integer.MIN_VALUE);
        return res;
    }

    public void dfs(TreeNode root, int min, int max) {
        if (root == null) {
            return;
        }
        min = Math.min(min, root.val);
        max = Math.max(max, root.val);

        res = Math.max(Math.max(Math.abs(root.val - min), Math.abs(root.val - max)), res);

        dfs(root.left, min, max);
        dfs(root.right, min, max);
    }
}
